import Taro, {
  useLoad,
  usePullDownRefresh,
  useReachBottom,
  useShareAppMessage,
  useShareTimeline,
  useTitleClick,
  useDidShow,
} from '@tarojs/taro';
import { userLogin } from '@/api/moredetail';

export const showerr = (message) => {
  // for (const key in message) {
    Taro.showToast({
      title: message,
      icon: 'none',
      duration: 1000,
    });
  // }
};

export function getTime(date) {
  date = new Date(date);
  var year = date.getFullYear(); // 年份
  var month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份（注意要加上1）
  var day = date.getDate().toString().padStart(2, '0'); // 天数
  var hours = date.getHours().toString().padStart(2, '0'); // 小时
  var minutes = date.getMinutes().toString().padStart(2, '0'); // 分钟
  var seconds = date.getSeconds().toString().padStart(2, '0'); // 秒数

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

export function geyDay(date) {
  date = new Date(date);
  var year = date.getFullYear(); // 年份
  var month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份（注意要加上1）
  var day = date.getDate().toString().padStart(2, '0'); // 天数
  return `${year}-${month}-${day}`;
}

export function loginuser(callfun) {
  let userinfo = Taro.getStorageSync('useInfo'); //用户信息
  let getid = userinfo && JSON.parse(userinfo)?.user_id || 4;
  console.log(getid)
  if (getid) {
    callfun();
  } else {
    Taro.login().then((res: any) => {
      let { code } = res;
      userLogin({ code }).then((res:any) => {
        res.user_id = res?.id

        const useInfo = Taro.setStorageSync('useInfo',JSON.stringify(res));
      });
    });
  }
}
